package com.allen.retrofit2_rxjava2.view.selectImage.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.allen.retrofit2_rxjava2.R;
import com.allen.retrofit2_rxjava2.utils.MainUtil;
import com.allen.retrofit2_rxjava2.view.selectImage.entity.ChooseUploadParam;
import com.bumptech.glide.Glide;
//import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.ViewTarget;
import com.bumptech.glide.request.transition.Transition;
import com.github.chrisbanes.photoview.PhotoView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lierdong on 2016/12/6.
 */
public class GalleryPagerAdapter extends PagerAdapter implements View.OnClickListener {

    private Context context;
    private List<View> views;
    private ArrayList<ChooseUploadParam> params;

    private OnPhotoItemClickListener onPhotoItemClickListener;
    private OnVideoItemClickListener onVideoItemClickListener;

    public GalleryPagerAdapter(Context context, ArrayList<ChooseUploadParam> params) {
        this.context = context;
        this.params = params;
        initData();
    }

    public ArrayList<ChooseUploadParam> getParams() {
        return params;
    }

    public void setOnPhotoItemClickListener(OnPhotoItemClickListener onPhotoItemClickListener) {
        this.onPhotoItemClickListener = onPhotoItemClickListener;
    }

    public void setOnVideoItemClickListener(OnVideoItemClickListener onVideoItemClickListener) {
        this.onVideoItemClickListener = onVideoItemClickListener;
    }

    private void initData() {
        views = new ArrayList<>();
        for (int i = 0; i < params.size(); i++) {
            ChooseUploadParam param = params.get(i);
            View child = View.inflate(context, R.layout.item_vp_gallery, null);
            ImageView iv_playTag = (ImageView) child.findViewById(R.id.iv_playTag);
            PhotoView photoView = (PhotoView) child.findViewById(R.id.pv);
            ProgressBar pb_loading = (ProgressBar) child.findViewById(R.id.pb_loading);
//            pb_loading.setVisibility(View.VISIBLE);
            photoView.setOnClickListener(this);
            switch (param.getType()) {
                case UploadVideoImageAdapter.TYPE_IMG:
                    iv_playTag.setVisibility(View.GONE);
                    try {
                        MainUtil.log(param.getPath());
                        Glide.with(context).
                                load(param.getPath())
                                .into(photoView);
                    } catch (Exception e) {
                    }
                    break;
                case UploadVideoImageAdapter.TYPE_VIDEO:
                    iv_playTag.setVisibility(View.VISIBLE);
                    String path = param.getPath();
                    iv_playTag.setOnClickListener(new MyclickListener(path));
                    String suffix = path.substring(path.lastIndexOf("."));
                    String thumbnailPath = path.replace(suffix, ".jpg");
                    Log.d("preview", suffix + ":" + thumbnailPath);
                    try {
                        Glide.with(context).
                                load(thumbnailPath).
                                into(photoView);
                    } catch (Exception e) {
                    }
                    break;
            }
            views.add(child);
        }
    }

    class LoadingViewTarget extends ViewTarget<ImageView, Bitmap> {

        private ImageView view;
        private ProgressBar pb_loading;

        public LoadingViewTarget(ImageView view, ProgressBar pb_loading) {
            super(view);
            this.view = view;
            this.pb_loading = pb_loading;
        }

        @Override
        public void onLoadFailed(Drawable errorDrawable) {
            super.onLoadFailed(errorDrawable);
            pb_loading.setVisibility(View.GONE);
        }

        @Override
        public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {

            view.setImageBitmap(resource);
            pb_loading.setVisibility(View.GONE);
        }
    }

    @Override
    public int getCount() {
        return params.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return object == view;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View child = views.get(position);
        container.addView(child);
        return child;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        View child = views.get(position);
        container.removeView(child);
    }

    @Override
    public void onClick(View v) {
        if (onPhotoItemClickListener != null) {
            onPhotoItemClickListener.onPhotoItemClick();
        }
    }

    class MyclickListener implements View.OnClickListener {

        private String path;

        public MyclickListener(String path) {
            this.path = path;
        }

        @Override
        public void onClick(View v) {
            if (onVideoItemClickListener != null) {
                onVideoItemClickListener.onVideoItemClick(path);
            }
        }
    }

    public interface OnVideoItemClickListener {

        void onVideoItemClick(String path);
    }

    public interface OnPhotoItemClickListener {

        void onPhotoItemClick();
    }
}
